<template>
    <div>
        <el-table :data="list" border style="width: 100%">
            <el-table-column prop="name" label="姓名" width="100"></el-table-column>
            <el-table-column prop="phone" label="电话" width="180"></el-table-column>
            <el-table-column prop="web" label="网址" width="180"></el-table-column>
            <el-table-column prop="note" label="备注"></el-table-column>
            <el-table-column prop="state" label="状态"></el-table-column>
            <el-table-column prop="addr" label="地址"></el-table-column>
            <el-table-column prop="comments" label="留言"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 弹出框 -->
        <div>
            <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose"
            >
                <span>修改信息</span>
                <el-form
                    :model="ruleForm"
                    :rules="rules"
                    ref="ruleForm"
                    label-width="100px"
                    class="demo-ruleForm"
                >
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="ruleForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="地址" prop="addr">
                        <el-input v-model="ruleForm.addr"></el-input>
                    </el-form-item>
                    <el-form-item label="电话" prop="phone">
                        <el-input v-model="ruleForm.phone"></el-input>
                    </el-form-item>
                    <el-form-item label="网址" prop="web">
                        <el-input v-model="ruleForm.web"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="note">
                        <el-input type="textarea" v-model="ruleForm.note"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { getListAll } from '../../../api/supplier';
import { mapState, MapActions, mapActions } from 'vuex';
import Axios from 'axios';
export default {
    data() {
        return {
            list: [],
            dialogVisible: false,
            ruleForm: {
                name: '',
                addr: '',
                phone: '',
                note: '',
                web: '',
                _id: ''
            }
        };
    },
    computed: {
        ...mapState('supplier', ['tableData'])
    },
    created() {
        this.getList();
    },
    methods: {
        handleEdit(index, row) {
            this.dialogVisible = true;
            this.ruleForm.name = row.name;
            this.ruleForm.addr = row.addr;
            this.ruleForm.phone = row.phone;
            this.ruleForm.note = row.note;
            this.ruleForm.web = row.web;
            this.ruleForm._id = row._id;
        },
        submitForm(formName, row) {
            this.dialogVisible = false;
            const _id = this.ruleForm._id;
            Axios({
                method: 'patch',
                url: `/api/supplier/${_id}`,
                data: {
                    name: this.ruleForm.name,
                    addr: this.ruleForm.addr,
                    phone: this.ruleForm.phone,
                    note: this.ruleForm.note,
                    web: this.ruleForm.web
                }
            }).then(res => {
                 this.getList();
            });
        },
        handleDelete(index, row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    Axios({
                        method: 'post',
                        url: '/api/supplier/delSupplier',
                        data: { _id: row._id }
                    }).then(res => {
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    });
                })
                .catch(() => {
                    
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        async getList() {
            const data = await getListAll();
            this.list = data.rows;
        }
    }
};
</script>

<style></style>
